package team.niit.service.impl;

import team.niit.bean.CourseBean;
import team.niit.dao.CourseDAO;
import team.niit.dao.impl.CourseDAOImpl;
import team.niit.entity.CourseEntity;
import team.niit.errorenum.ErrorCodeEnum;
import team.niit.service.CourseService;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: JSF
 * @Package_name: team.niit.service.impl
 * @Date: 2021/10/26 20:02
 */
public class CourseServiceImpl implements CourseService {

    private CourseDAO courseDAO = new CourseDAOImpl();

    @Override
    public List<CourseEntity> list() {
        return courseDAO.selectAll();
    }

    @Override
    public boolean addCourse(CourseEntity courseEntity) {
        return courseDAO.insertCourse(courseEntity);
    }

    @Override
    public boolean modifyCourse(CourseEntity courseEntity) {
        return courseDAO.updateCourse(courseEntity);
    }

    @Override
    public boolean deleteCourseById(String id) {
        if(courseDAO.deleteCourseById(id)){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<CourseEntity> findOptionalCoursesList() {
        return courseDAO.selectOptionalCoursesList();
    }

    @Deprecated
    @Override
    public List<CourseEntity> pagingQuery(int size, int pageNumber) {
        return courseDAO.pagingQuery(size, pageNumber);
    }

    @Override
    public int count() {
        return courseDAO.count();
    }

    @Override
    public boolean isExist(String courseId, String studentId) {
        return courseDAO.isExist(courseId, studentId);
    }

    @Override
    public ErrorCodeEnum selectCourse(String courseId, String studentId) {
        // 检查课程余额
        if (courseDAO.checkQuota(courseId)) {
            // 已选三门课程，不能再选
            if (courseDAO.countCourse(studentId) > 2) {
                return ErrorCodeEnum.OUT_OF_BOUNDS;
            }
            // 该课程已被选择
            if (courseDAO.isExist(courseId, studentId)) {
                return ErrorCodeEnum.HAS_BEEN_SELECTED;
            } else {
                if (courseDAO.isNull(courseId, studentId)) {
                    // 选课
                    try {
                        courseDAO.selectCourse(courseId, studentId);
                    } catch (Exception e) {
                        // 内部错误
                        return ErrorCodeEnum.INNER_ERROR;
                    }
                    // 选课成功
                    return ErrorCodeEnum.SUCCESS;
                } else {
                    // 这节课已经有课了
                    return ErrorCodeEnum.EXIST_OTHER_COURSE;
                }
            }
        } else {
            return ErrorCodeEnum.INSUFFICIENT;
        }
    }

    @Override
    public CourseEntity[][] queryAllCourse(String id) {
        return courseDAO.selectAllCourse(id);
    }

    @Override
    public List<CourseEntity> findSelectedCourseList(String studentId) {
        List<CourseBean> courseBeanList = new ArrayList<>();
        return courseDAO.selectSelectedCourse(studentId);
    }

    @Override
    public int dropCourse(String courseId, String studentId) {
        if (courseDAO.dropCourse(courseId, studentId)) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public CourseEntity getById(String id) {
        return courseDAO.selectById(id);
    }


    @Override
    public List<CourseEntity> getStudentCourseById(String studentId) {
        return courseDAO.queryStudentCourse(studentId);
    }

    @Override
    public boolean deleteStudentCourse(String studentId, String courseId) {
        if(courseDAO.deleteStudentCourse(studentId,courseId)){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean addStudentCourse(String studentId, String courseId) {
        return courseDAO.insertStudentCourse(studentId,courseId);
    }
}
